package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void addCart(ShoppingCartDTO dto) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();

        //1.构造购物车对象数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);

        //2.将购物车添加到shopping_cart表中
        //添加购物车时，需要考虑该商品是否已经存在了
        //查询购物车数据，根据什么条件：用户id+套餐id(菜品id+口味)
        shoppingCart.setUserId(userId);
        ShoppingCart cartDB = shoppingCartMapper.getBy(shoppingCart);
        if (cartDB != null) {
            // 如果已存在直接数量加1
            cartDB.setNumber(cartDB.getNumber() + 1);
            shoppingCartMapper.update(cartDB);
        }else {
            //否则执行新增操作
            if (dto.getDishId() != null) {  //代表新增的是菜品
                Dish dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
            }else { //代表新增的是套餐
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();

        //调用mapper的查询方法，根据用户id查询
        return shoppingCartMapper.getByUserId(userId);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cartDB = shoppingCartMapper.getBy(shoppingCart);

        if(cartDB != null){
            Integer number = cartDB.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(cartDB.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(cartDB.getNumber() - 1);
                shoppingCartMapper.update(cartDB);
            }
        }
    }
}
